package com.ilop.siteraudio.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.data.bean.EmailBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class SendReportAdapter extends RecyclerView.Adapter<SendReportAdapter.ItemHolder> {

    private final Context mContext;
    private int mEmailSize;
    private boolean mInputSuccess = false;
    private final List<Integer> mSelected;
    private final List<EmailBean> mEmailLists;
    private final Map<Integer, String> mEmailList;

    public SendReportAdapter(Context context, List<EmailBean> emailList) {
        this.mContext = context;
        this.mEmailLists = emailList;
        mSelected = new ArrayList<>();
        mEmailList = new HashMap<>();
        mEmailSize = emailList.size();
        if (mEmailSize == 0) {
            mSelected.add(0);
            mEmailLists.add(new EmailBean());
        }else {
            for (int i = 0; i < mEmailSize; i++) {
                mSelected.add(i);
            }
        }
    }

    public void onAddNewEmail() {
        int position = mSelected.size();
        //在list中添加数据，并通知条目加入一条
        mSelected.add(position);
        mEmailLists.add(new EmailBean());
        //添加动画
        notifyItemInserted(position);
    }

    //  删除数据
    public void removeData(int position) {
        mSelected.remove(position);
        mEmailLists.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public List<String> getEmailAddressList() {
        List<String> email = new ArrayList<>();
        for (Map.Entry<Integer, String> entry : mEmailList.entrySet()) {
            email.add(entry.getValue());
        }
        return email;
    }
    
    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_send_report, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        itemHolder.editText_email.setText(mEmailLists.get(i).getEmail());
        itemHolder.editText_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mInputSuccess = isEmail(editable.toString());
                itemHolder.checkBox_box.setChecked(mInputSuccess);
            }
        });
        itemHolder.checkBox_box.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                String email = itemHolder.editText_email.getText().toString();
                mEmailLists.get(i).setEmail(email);
                mEmailList.put(i, email);
            } else {
                mEmailList.remove(i);
            }
        });
        itemHolder.imageView_close.setOnClickListener(view -> {
            if (i == 0) {
                itemHolder.editText_email.setText("");
            }else {
                removeData(i);
                mEmailList.remove(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mSelected == null ? 0 : mSelected.size();
    }

    public static class ItemHolder extends RecyclerView.ViewHolder {
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_send_close)
        ImageView imageView_close;
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_send_email)
        EditText editText_email;
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_send_box)
        CheckBox checkBox_box;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //邮箱验证
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        if (TextUtils.isEmpty(strPattern)) {
            return false;
        } else {
            return strEmail.matches(strPattern);
        }
    }

    public interface onSendReportItemClick{

        void onEmailAddressInput(int position, String email);
    }
}